import {
  createRouter,
  createWebHashHistory,
  createWebHistory
} from "vue-router"

import layout from '../views/layout/index.vue'
import routerPlaceholder from '../views/layout/router-placeholder.vue'

import earth from './earth/index.js'
import effect from './effect/index.js'
import effects from './effects/index.js'
import terrain from './terrain/index.js'
import dataSource from './dataSource/index.js'
import models from './models/index.js'
import textures from './textures/index.js'
import views from './views/index.js'
import marks from './marks/index.js'
import animate from './animate/index.js'
import apps from './apps/index.js'
import data from './data/index.js'
import functional from './functional/index.js'

let name2path = {};

function updatePath(route, base) {
  let path = route.path;
  let item = name2path[route.name]
  if (item) {
    console.warn(`exists`, item)
  }
  if (!path || path === '*') {
    name2path[route.name] = path
    return;
  }
  base = base || '/'
  if (!base.endsWith('/')) {
    base = base + "/"
  }
  if (!path.startsWith("/")) {
    path = base + path
  }
  route.path = path;

  name2path[route.name] = route.path

  let children = route.children;
  if (children) {
    for (let i = 0; i < children.length; ++i) {
      updatePath(children[i], path)
    }
  }
}

function updateRoutesPath(routes, base) {
  if (routes) {
    for (let i = 0; i < routes.length; ++i) {
      updatePath(routes[i], base)
    }
  }
}

let constantRoutes = [{
  path: '/',
  name: 'root',
  redirect: {
    name: "globe"
  },
  component: layout,
  children: [{
    path: 'rotate',
    name: 'rotate',
    component: () => import('@/views/pages/globe-rotate'),
    meta: {
      activeMenu: 'rotate',
      title: 'rotate',
      icon: 'rotate',
      affix: true
    }
  },{
    path: 'tdt',
    name: 'tdt',
    component: () => import('@/views/pages/tdt'),
    meta: {
      activeMenu: 'tdt',
      title: 'tdt',
      icon: 'dashboard',
      affix: true
    }
  },{
    path: 'flyTracker',
    name: 'flyTracker',
    component: () => import('@/views/pages/flyTracker'),
    meta: {
      activeMenu: 'flyTracker',
      title: 'flyTracker',
      icon: 'dashboard',
      affix: true
    }
  },{
    path: 'volcano',
    name: 'volcano',
    component: () => import('@/views/pages/volcano'),
    meta: {
      activeMenu: 'volcano',
      title: 'volcano',
      icon: 'volcano',
      affix: true
    }
  },{
    path: 'windyField',
    name: 'windyField',
    component: () => import('@/views/pages/windyField'),
    meta: {
      activeMenu: 'windyField',
      title: 'windyField',
      icon: 'windyField',
      affix: true
    }
  },

  {
    path: 'basic',
    name: 'basic',
    component: () => import('@/views/functional/basic.vue'),
    meta: {
      activeMenu: 'basic',
      title: 'basic',
      icon: 'basic',
      affix: true
    }
  },{
    path: 'measure',
    name: 'measure',
    component: () => import('@/views/functional/measure.vue'),
    meta: {
      activeMenu: 'measure',
      title: 'measure',
      icon: 'measure',
      affix: true
    }
  },{
    path: 'profile',
    name: 'profile',
    component: () => import('@/views/functional/profile/index.vue'),
    meta: {
      activeMenu: 'profile',
      title: 'profile',
      icon: 'profile',
      affix: true
    }
  },{
    path: 'viewShed',
    name: 'viewShed',
    component: () => import('@/views/functional/viewShed/index.vue'),
    meta: {
      activeMenu: 'viewShed',
      title: 'viewShed',
      icon: 'viewShed',
      affix: true
    }
  },{
    path: 'layerControl',
    name: 'layerControl',
    component: () => import('@/views/functional/LayerControl/index.vue'),
    meta: {
      activeMenu: 'layerControl',
      title: 'layerControl',
      icon: 'layerControl',
      affix: true
    }
  },{
    path: 'mapOptions',
    name: 'mapOptions',
    component: () => import('@/views/functional/mapOptions/index.vue'),
    meta: {
      activeMenu: 'mapOptions',
      title: 'mapOptions',
      icon: 'mapOptions',
      affix: true
    }
  },{
    path: 'flowLine',
    name: 'flowLine',
    component: () => import('@/views/functional/flowLine/index.vue'),
    meta: {
      activeMenu: 'flowLine',
      title: 'flowLine',
      icon: 'flowLine',
      affix: true
    }
  },{
    path: 'flowLine2',
    name: 'flowLine2',
    component: () => import('@/views/functional/flowLine2/index.vue'),
    meta: {
      activeMenu: 'flowLine2',
      title: 'flowLine2',
      icon: 'flowLine2',
      affix: true
    }
  },{
    path: 'flyLine',
    name: 'flyLine',
    component: () => import('@/views/functional/flyLine/index.vue'),
    meta: {
      activeMenu: 'flyLine',
      title: 'flyLine',
      icon: 'flyLine',
      affix: true
    }
  },
  earth,
  animate,
  apps,
  effect,
  marks,
  effects,
  terrain,
  dataSource,
  models,
  textures,
  views,
  data,
  functional
  ]
}]


updateRoutesPath(constantRoutes, '/')

const routes = constantRoutes;

// 3. 创建路由实例
const router = createRouter({
  // 4. 采用hash 模式
  history: createWebHashHistory(),
  // 采用 history 模式
  // history: createWebHistory(),
  routes, // short for `routes: routes`
});

export function pathFromRouteName(name) {
  return name2path[name]
}

export default router
